<template>
  <div class="app-container home">
    <!-- 数据面板区域 -->
    <el-row :gutter="20" class="dashboard-section">
      <el-col :span="24">
        <div class="section-title">
          <i class="el-icon-data-analysis"></i>
          <span>数据面板</span>
        </div>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6" :lg="6">
        <el-card class="dashboard-card user-card">
          <div class="card-content">
            <div class="card-icon">
              <i class="el-icon-user"></i>
            </div>
            <div class="card-info">
              <div class="card-number">{{ dashboardData.userCount }}</div>
              <div class="card-label">用户总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6" :lg="6">
        <el-card class="dashboard-card dept-card">
          <div class="card-content">
            <div class="card-icon">
              <i class="el-icon-office-building"></i>
            </div>
            <div class="card-info">
              <div class="card-number">{{ dashboardData.deptCount }}</div>
              <div class="card-label">部门数量</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6" :lg="6">
        <el-card class="dashboard-card notice-card">
          <div class="card-content">
            <div class="card-icon">
              <i class="el-icon-bell"></i>
            </div>
            <div class="card-info">
              <div class="card-number">{{ dashboardData.noticeCount }}</div>
              <div class="card-label">通知公告</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6" :lg="6">
        <el-card class="dashboard-card course-card">
          <div class="card-content">
            <div class="card-icon">
              <i class="el-icon-reading"></i>
            </div>
            <div class="card-info">
              <div class="card-number">{{ dashboardData.courseCount }}</div>
              <div class="card-label">课程数量</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 学校接收区域 -->
    <el-row :gutter="20" class="school-section">
      <el-col :span="24">
        <div class="section-title">
          <i class="el-icon-school"></i>
          <span>学校接收</span>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <el-card class="school-card">
          <div slot="header" class="clearfix">
            <span>最新通知</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="viewAllNotices">查看全部</el-button>
          </div>
          <div class="notice-list">
            <div v-for="notice in recentNotices" :key="notice.noticeId" class="notice-item" @click="viewNotice(notice)">
              <div class="notice-title">{{ notice.noticeTitle }}</div>
              <div class="notice-time">{{ formatTime(notice.createTime) }}</div>
            </div>
            <div v-if="recentNotices.length === 0" class="no-data">
              <i class="el-icon-document"></i>
              <span>暂无通知</span>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <el-card class="school-card">
          <div slot="header" class="clearfix">
            <span>系统公告</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="viewAllAnnouncements">查看全部</el-button>
          </div>
          <div class="announcement-content">
            <div class="welcome-message">
              <h3>欢迎使用智教云管理系统</h3>
              <p>智教云是一款现代化的教育管理系统，为学校提供全方位的信息化管理解决方案。</p>
              <ul>
                <li>✓ 用户权限管理</li>
                <li>✓ 教务管理系统</li>
                <li>✓ 数据可视化分析</li>
                <li>✓ 智能排课系统</li>
              </ul>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 常用应用区域 -->
    <el-row :gutter="20" class="apps-section">
      <el-col :span="24">
        <div class="section-title">
          <i class="el-icon-menu"></i>
          <span>常用应用</span>
        </div>
      </el-col>
      <el-col :xs="12" :sm="8" :md="6" :lg="4" v-for="app in commonApps" :key="app.id">
        <el-card class="app-card" @click.native="openApp(app)">
          <div class="app-content">
            <div class="app-icon">
              <i :class="app.icon"></i>
            </div>
            <div class="app-name">{{ app.name }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getDashboardData, getRecentNotices } from "@/api/dashboard"

export default {
  name: "Index",
  data() {
    return {
      // 仪表板数据
      dashboardData: {
        userCount: 0,
        deptCount: 0,
        noticeCount: 0,
        courseCount: 0
      },
      // 最近通知
      recentNotices: [],
      // 常用应用
      commonApps: [
        {
          id: 1,
          name: "用户管理",
          icon: "el-icon-user",
          path: "/system/user"
        },
        {
          id: 2,
          name: "角色管理",
          icon: "el-icon-s-custom",
          path: "/system/role"
        },
        {
          id: 3,
          name: "菜单管理",
          icon: "el-icon-menu",
          path: "/system/menu"
        },
        {
          id: 4,
          name: "部门管理",
          icon: "el-icon-office-building",
          path: "/system/dept"
        },
        {
          id: 5,
          name: "通知公告",
          icon: "el-icon-bell",
          path: "/system/notice"
        },
        {
          id: 6,
          name: "操作日志",
          icon: "el-icon-document",
          path: "/monitor/operlog"
        },
        {
          id: 7,
          name: "课程管理",
          icon: "el-icon-reading",
          path: "/educational/course"
        },
        {
          id: 8,
          name: "教师管理",
          icon: "el-icon-user-solid",
          path: "/educational/teacher"
        },
        {
          id: 9,
          name: "智能排课",
          icon: "el-icon-date",
          path: "/scheduling/task"
        },
        {
          id: 10,
          name: "数据分析",
          icon: "el-icon-data-analysis",
          path: "/dashboard/analysis"
        },
        {
          id: 11,
          name: "系统监控",
          icon: "el-icon-monitor",
          path: "/monitor/server"
        },
        {
          id: 12,
          name: "代码生成",
          icon: "el-icon-cpu",
          path: "/tool/gen"
        }
      ]
    };
  },
  created() {
    this.loadDashboardData();
    this.loadRecentNotices();
  },
  methods: {
    /**
     * 加载仪表板数据
     */
    async loadDashboardData() {
      try {
        const response = await getDashboardData();
        if (response.code === 200) {
          this.dashboardData = response.data;
        }
      } catch (error) {
        // 如果API不存在，使用模拟数据
        this.dashboardData = {
          userCount: 3,
          deptCount: 10,
          noticeCount: 2,
          courseCount: 15
        };
      }
    },
    
    /**
     * 加载最近通知
     */
    async loadRecentNotices() {
      try {
        const response = await getRecentNotices();
        if (response.code === 200) {
          this.recentNotices = response.rows || [];
        }
      } catch (error) {
        // 如果API不存在，使用模拟数据
        this.recentNotices = [
          {
            noticeId: 1,
            noticeTitle: "系统维护通知",
            createTime: new Date()
          },
          {
            noticeId: 2,
            noticeTitle: "新功能上线公告",
            createTime: new Date(Date.now() - 24 * 60 * 60 * 1000)
          }
        ];
      }
    },
    
    /**
     * 打开应用
     */
    openApp(app) {
      this.$router.push(app.path);
    },
    
    /**
     * 查看通知详情
     */
    viewNotice(notice) {
      this.$router.push({
        path: '/system/notice',
        query: { noticeId: notice.noticeId }
      });
    },
    
    /**
     * 查看所有通知
     */
    viewAllNotices() {
      this.$router.push('/system/notice');
    },
    
    /**
     * 查看所有公告
     */
    viewAllAnnouncements() {
      this.$router.push('/system/notice');
    },
    
    /**
     * 格式化时间
     */
    formatTime(time) {
      if (!time) return '';
      const date = new Date(time);
      const now = new Date();
      const diff = now - date;
      const days = Math.floor(diff / (24 * 60 * 60 * 1000));
      
      if (days === 0) {
        return '今天';
      } else if (days === 1) {
        return '昨天';
      } else if (days < 7) {
        return `${days}天前`;
      } else {
        return date.toLocaleDateString();
      }
    }
  }
};
</script>

<style scoped lang="scss">
.home {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: calc(100vh - 84px);
  
  .section-title {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 20px;
    padding-left: 10px;
    border-left: 4px solid #409EFF;
    
    i {
      margin-right: 8px;
      color: #409EFF;
    }
  }
  
  .dashboard-section {
    margin-bottom: 30px;
    
    .dashboard-card {
      height: 120px;
      cursor: pointer;
      transition: all 0.3s;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      }
      
      .card-content {
        display: flex;
        align-items: center;
        height: 100%;
        
        .card-icon {
          width: 60px;
          height: 60px;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 20px;
          
          i {
            font-size: 28px;
            color: white;
          }
        }
        
        .card-info {
          flex: 1;
          
          .card-number {
            font-size: 32px;
            font-weight: bold;
            color: #303133;
            line-height: 1;
          }
          
          .card-label {
            font-size: 14px;
            color: #909399;
            margin-top: 8px;
          }
        }
      }
      
      &.user-card .card-icon {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }
      
      &.dept-card .card-icon {
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
      }
      
      &.notice-card .card-icon {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }
      
      &.course-card .card-icon {
        background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
      }
    }
  }
  
  .school-section {
    margin-bottom: 30px;
    
    .school-card {
      height: 300px;
      
      .notice-list {
        max-height: 220px;
        overflow-y: auto;
        
        .notice-item {
          padding: 12px 0;
          border-bottom: 1px solid #f0f0f0;
          cursor: pointer;
          transition: all 0.3s;
          
          &:hover {
            background-color: #f8f9fa;
            padding-left: 10px;
          }
          
          &:last-child {
            border-bottom: none;
          }
          
          .notice-title {
            font-size: 14px;
            color: #303133;
            margin-bottom: 5px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          
          .notice-time {
            font-size: 12px;
            color: #909399;
          }
        }
        
        .no-data {
          text-align: center;
          padding: 40px 0;
          color: #909399;
          
          i {
            font-size: 48px;
            margin-bottom: 10px;
            display: block;
          }
        }
      }
      
      .announcement-content {
        .welcome-message {
          h3 {
            color: #303133;
            margin-bottom: 15px;
            font-size: 16px;
          }
          
          p {
            color: #606266;
            line-height: 1.6;
            margin-bottom: 15px;
          }
          
          ul {
            list-style: none;
            padding: 0;
            
            li {
              color: #606266;
              line-height: 1.8;
              font-size: 14px;
              
              &:before {
                content: "";
                margin-right: 8px;
              }
            }
          }
        }
      }
    }
  }
  
  .apps-section {
    .app-card {
      height: 100px;
      cursor: pointer;
      transition: all 0.3s;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        
        .app-icon i {
          color: #409EFF;
        }
      }
      
      .app-content {
        text-align: center;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        
        .app-icon {
          margin-bottom: 10px;
          
          i {
            font-size: 32px;
            color: #909399;
            transition: color 0.3s;
          }
        }
        
        .app-name {
          font-size: 14px;
          color: #303133;
          font-weight: 500;
        }
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .home {
    padding: 10px;
    
    .dashboard-section .dashboard-card {
      margin-bottom: 15px;
    }
    
    .school-section .school-card {
      margin-bottom: 15px;
    }
    
    .apps-section .app-card {
      margin-bottom: 15px;
    }
  }
}
</style>

